/**
 * 
 */
package easyservice.application;

import junit.framework.TestCase;
import mockapp.SampleClientApp;
import mockapp.SampleClientAppImpl;
import mockapp.SampleServerApp;
import mockapp.SampleServerAppImpl;
import mockapp.SampleServerListener;

/**
 * @author Rodrigo Barbosa Lira
 * 
 */
public class ServerAgentImplTest extends TestCase {

	private SampleServerApp serverApp;

	@SuppressWarnings("unused")
	private ServerAgent<SampleServerApp, SampleServerListener> serverAgent;

	private ClientAgentImpl<SampleServerApp, SampleServerListener> clientAgent1;
	
	@SuppressWarnings("unused")
	private ClientAgentImpl<SampleServerApp, SampleServerListener> clientAgent2;

	private SampleClientApp c1;

	private SampleClientApp c2;

	@Override
	protected void setUp() throws Exception {
		serverApp = new SampleServerAppImpl();
		c1 = new SampleClientAppImpl();
		c2 = new SampleClientAppImpl();
		
		serverAgent = new ServerAgentImpl<SampleServerApp, SampleServerListener>("server1", "server1", "zezinho", 5222, serverApp, SampleServerListener.class);
		clientAgent1 = new ClientAgentImpl<SampleServerApp, SampleServerListener>("client1", "client1", "zezinho", 5222, "server1@zezinho", SampleServerApp.class, c1);
		clientAgent2 = new ClientAgentImpl<SampleServerApp, SampleServerListener>("client2", "client2", "zezinho", 5222, "server1@zezinho", SampleServerApp.class, c2);
	}
	
	@Override
	protected void tearDown() throws Exception {
		serverAgent = null;
		clientAgent1 = null;
		clientAgent2 = null;
		c1 = null;
		c2 = null;
	}

	public void testSync() {
		SampleServerApp proxyApp = (SampleServerApp) clientAgent1.getFacade();
		assertNotNull(proxyApp);
		try {
			proxyApp.method1();
		} catch (Throwable e) {
			fail(e.getMessage());
		}
		assertTrue(proxyApp.method2() == 'c');
		assertEquals("string", proxyApp.method3());
		assertTrue(proxyApp.method4() == 25);
		assertTrue(proxyApp.method5() == 25);
		assertTrue(proxyApp.method6() == 25.0f);
		assertTrue(proxyApp.method7() == 25.0d);
		
		assertNull(proxyApp.method8());
	}
	
	public void testAsync() throws InterruptedException {
		assertFalse(c1.isVar());
		assertTrue(c1.getCh() == 0);
		assertNull(c1.getText());
		
		assertFalse(c2.isVar());
		assertTrue(c2.getCh() == 0);
		assertNull(c2.getText());
		
		serverApp.addListener(serverAgent.getNotifiableInterface());
		
		// notify 1
		serverApp.notify1();
//		serverApp.notify2('a');
		serverApp.notify3("text");
		Thread.sleep(100);
			
		assertTrue(c1.isVar());
//		assertTrue(c1.getCh() == 'z');
		assertEquals("text", c1.getText());
		
		
		assertTrue(c2.isVar());
//		assertTrue(c1.getCh() == 'a');
		assertEquals("text", c2.getText());
	}
}
